package logic

import (
	"context"
	"month2/C/rpc_C/model/mysql"
	"month2/C/rpc_C/model/redis"

	"month2/C/rpc_C/internal/svc"
	"month2/C/rpc_C/order"

	"github.com/zeromicro/go-zero/core/logx"
)

type RankGoodsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRankGoodsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RankGoodsLogic {
	return &RankGoodsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RankGoodsLogic) RankGoods(in *order.RankGoodsRequest) (*order.RankGoodsResponse, error) {
	g := mysql.Goods1{}
	err := g.RankType(int(in.GoodsType))
	if err != nil {
		return nil, err
	}
	rank := redis.GetRank()
	var list []*order.GoodsListType
	for _, z := range rank {
		listType := order.GoodsListType{
			Score:  float32(z.Score),
			Member: z.Member.(string),
		}
		list = append(list, &listType)
	}
	return &order.RankGoodsResponse{
		List: list,
	}, nil
}
